/**
 * Copyright (C) 2010 kt innotz Co., Ltd. All rights reserved.
 */
package com.kt.paas.prov.iaas.listeners;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ktinnotz.iaas.internal.AsyncJobWaiter;
import com.ktinnotz.iaas.services.IaasEvent;
import com.ktinnotz.iaas.services.IaasEventListener;
import com.ktinnotz.iaas.services.IaasJobContext;

/**
 * Created on 2011. 2. 18.
 * <p>Title : DefaultIaaSEventListener.java</p>
 * <p>@author : kobe </p>
 * <p>Description : </p>
 * 
 *
 */
public class DefaultIaaSEventListener extends IaasEventListener {

	protected static final Logger logger = LoggerFactory.getLogger(DefaultIaaSEventListener.class);
	
	protected AsyncJobWaiter job;
	
	public DefaultIaaSEventListener() {}
	
	public DefaultIaaSEventListener( AsyncJobWaiter job ) {
		this.job = job;
	}
	
	/* (non-Javadoc)
	 * @see com.ktinnotz.iaas.services.IaasEventListener#eventChanged(com.ktinnotz.iaas.services.IaasJobContext, com.ktinnotz.iaas.services.IaasEvent)
	 */
	@Override
	public int eventChanged(IaasJobContext context, IaasEvent event) {
		switch ( event.getType() ) {
		case IaasEvent.JOB_COMPLETED:
			logger.info( "IaasEvent.JOB_COMPLETED" );
			break;
		case IaasEvent.EXCEPTION_UNKNOWN:
			logger.info( "IaasEvent.EXCPT_UNKNOWN" );
			break;
		case IaasEvent.RESULT_TIMEOUT:
			logger.info( "IaasEvent.RESULT_TIMEOUT" );
			break;
		default:
		}
		printData( event.getData() );
		if ( job != null )
			job.end();
		return 0;
	}

	private void printData( Object data ) {
		if ( data == null ) {
			return;
		}
		else if ( data instanceof Exception ) {
			Exception exception = (Exception)data;
			logger.info("error", exception);
		}
		else {
			logger.info("error", data.getClass());
		}
	}
}
